function initBar(id,Xdata,Ydata,title){
    if(Xdata.length > 0){
        var barChart = echarts.init(document.getElementById(id));
        // app.title = '坐标轴刻度与标签对齐';
        option = {
            backgroundColor: '#ffffff',
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'shadow'
                }
            },
            title: {
                text: title,
                textStyle: {
                    color: "#666666",
                    fontSize:16
                }
            },
            grid: {
                left: '3%',
                right: '4%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [{
                type: 'category',
                // data: [1,2],
                data: Xdata,
                axisLine: {
                    show: false
                },
                axisTick: {
                    show: false,
                },
                axisLabel: {
                    show: true,
                    textStyle: {
                        color: "#666666",
                    }
                }
            }],
            yAxis: [{
                type: 'value',
                minInterval : 1,
                axisLabel: {
                    textStyle: {
                        color: "#666666",
                    },
                    formatter: '{value}'
                },
                axisTick: {
                    show: false,
                },
                axisLine: {
                    show: false,
                },
                splitLine: {
                    show:false
                }
            }],
            series: [{
                name: '接入率',
                type: 'bar',
                data: Ydata,
                barGap:'-100%',
                // barCategoryGap:'40%',
                barWidth: 19, //柱子宽度
                // barGap: '-135%', //柱子之间间距,
                label: {
                    normal: {
                        show: true,
                        position: 'top',
                        textStyle: {
                            color: "#666666",
                        }
                    }
                },
                itemStyle: {
                    normal: {
                        barBorderRadius: 5,
                        color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
                            offset: 1,
                            color: '#7f7fd5'
                        }, {
                            offset: 0,
                            color: '#30afff'
                        }]),
                        opacity: 1
                    }
                }
            }]
        };
        barChart.setOption(option, true);


    }
    else{
        var barChart = echarts.init(document.getElementById(id));
        option = {
            title: {
                text: '暂无数据!!!',
                textStyle: {
                    fontSize: 25,
                    fontWeight: 'normal',
                    color: '#666',
                    textAlign:'center',
                    marginTop:'50'
                }

            }
        };
        barChart.setOption(option, true);
    }



}
function initPie(id,test,abnormal,normal){
    if(test <= 0){

        var pieChart = echarts.init(document.getElementById(id));
        option = {
            title: {
                text: '暂无数据!!!',
                textStyle: {
                    fontSize: 25,
                    fontWeight: 'normal',
                    color: '#666',
                    textAlign:'center',
                    marginTop:'50'

                }

            }
        };
        pieChart.setOption(option, true);
        // $('#'+id).html('<h1 class="noData">暂无数据!!!</h1>')
        // return;
    }else{
        // $('#'+id).html('');
        // debugger;
        var ratio = abnormal/test;
        var result = ratio.toFixed(2);
        var pieChart = echarts.init(document.getElementById(id));
        var dataStyle = {
            normal: {
                label: {
                    show: false
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
            }
        };

        var labelShow = {
            show: false
        };

        var placeHolderStyle = {
            normal: {
                color: '#11b8ff',
                label: {
                    show: false
                },
                labelLine: {
                    show: false
                }
            }
        };
        option = {
            title: {
                text: '',
                textStyle: {
                    fontSize: 16,
                    fontWeight: 'normal',
                    color: '#666'
                },
                x: 'left',
                y:'-5'

            },
            color: ['#11b8ff', '#3a85ff','transparent'],
            // tooltip: {
            //     show: true,
            //     formatter: "{b} <br/> {c} ({d}%)"
            // },
            angleAxis: {
                type: 'category',
                z: 10,
                axisLine: {
                    show:false
                },
            },
            radiusAxis: {
                axisTick: {
                    show: false
                },
                axisLabel: {
                    show: false

                },
                axisLine: {
                    show: false
                },
                splitLine: {
                    show:false
                },

            },
            polar: {
                radius: 140,
            },
            legend: {
                top: '70',
                right:43,
                orient:'vertical',
                textStyle: {
                    fontSize: 14,
                    color: '#666666'
                },
                itemGap: 18,
                // data: ['总人数:250','一类烟', '二类烟'],
                data:[
                    {
                        name:'总人数 : '+test,
                    },
                    {
                        // name:'正常人数' + ' : '+ normal
                        name:'达标人数' + ' : '+ normal
                    },
                    {
                        // name:'偏高人数' + ' : '+ abnormal + '(' + parseInt(result*100) + '%)'
                        name:'未达标人数' + ' : '+ abnormal + '(' + parseInt(result*100) + '%)'
                    }

                ]

            },
            series: [{
                name: 'Line 1',
                type: 'pie',
                clockWise: false,
                center:['90','95'],
                radius: [43, 73],
                itemStyle: dataStyle,
                hoverAnimation: true,
                toolbox: {
                    feature: {
                        restore: {},
                        saveAsImage: {}
                    }
                },
                data: [{
                    value: normal,
                    name: '',
                    itemStyle: {
                        normal: {
                            color: 'rgba(0,0,0,0)',
                            label: {
                                show: false
                            },
                            labelLine: {
                                show: false
                            }
                        }
                    }
                },
                    {
                        value: abnormal,
                        // name:'偏高人数' + ' : '+ abnormal + '(' + result*100 + '%)',
                        name:'未达标人数' + ' : '+ abnormal + '(' + result*100 + '%)',
                        label: labelShow
                    }
                ]
            },
                {
                    name: 'Line 2',
                    type: 'pie',
                    clockWise: false,
                    center:['90','95'],
                    radius: [43, 69],
                    itemStyle: dataStyle,
                    hoverAnimation: true,

                    data: [
                        {
                            value: normal,
                            // name:'正常人数' + ' : '+ normal,
                            name:'达标人数' + ' : '+ normal,
                            formatter: function(name){
                                return name + '123'
                            },
                            label: labelShow,
                        },
                        {
                            value: abnormal,
                            name: '',
                            itemStyle: {
                                normal: {
                                    color: 'rgba(0,0,0,0)',
                                    label: {
                                        show: false
                                    },
                                    labelLine: {
                                        show: false
                                    }
                                }
                            }
                        }
                    ]
                }
                ,
                {
                    name: '总人数 : '+test,
                    type: 'pie',
                    clockWise: false,
                    center:['90','95'],
                    radius: [30, 40],
                    itemStyle: {
                        normal: {
                            label: {
                                show: false
                            },
                            labelLine: {
                                normal: {
                                    show: false
                                }
                            },
                        }
                    },
                    hoverAnimation: false,
                    data: [
                        {
                            value: test,
                            name: '总人数 : '+ test,

                        },
                        {
                            value: 0,
                            name: ''

                        }
                    ]
                }

            ]
        };


        pieChart.setOption(option, true);
    }

}

function getChartData(ids){
    var bsError = [];
    var abnormalAgeAxis = [];
    var abnormalAgeYxis = [];
    var abnormalSugarAgeAxis = [];
    var abnormalSugarAgeYxis = [];
    var bpTestNum = '';
    var bpTestErrorNum = '';
    var bpTestNormalNum = '';
    var bsBeforeTestErrorNum ='';
    var bsBeforeTestNum = '';
    var bsAfterTestNum = '';
    var bsAfterTestErrorNum ='';
    var bsBeforeNormal = '';
    var bsAfterNormal = '';
    var selectId = [];
    var selectEle = [];

    selectEle = $('.ms-drop .selected input');
    for(var i = 0;i<selectEle.length;i++){
        if(selectEle[i].id){
            selectId.push(selectEle[i].id);
        }
    }

    $.ajax({
        url:ctx + '/index/count3',
        type:'post',
        async : true, //默认为true 异步
        data:{
            ids:selectId.toString()
        },
        dataType : 'json',
        error:function(data){
            alert('error');
        },
        success:function(res) {
            $('.floor3 .bpNum').html(res.data.bpNum);
            $('.floor3 .bsNum').html(res.data.bsNum);
            $('.floor3 .bp60Num').html(res.data.bp60Num);
            $('.floor3 .bs60Num').html(res.data.bs60Num);
            $('.floor3 .manNum').html(res.data.manNum);
            $('.floor3 .womanNum').html(res.data.womanNum);
            bpTestNum = res.data.bpTestNum;
            bpTestErrorNum = res.data.bpTestErrorNum;
            bpTestNormalNum = bpTestNum - bpTestErrorNum;
            // 餐前血糖
            bsBeforeTestErrorNum = res.data.bsBeforeTestErrorNum;
            bsBeforeTestNum = res.data.bsBeforeTestNum;
            bsBeforeNormal = bsBeforeTestNum - bsBeforeTestErrorNum;
            // 餐后血糖
            bsAfterTestNum = res.data.bsAfterTestNum;
            bsAfterTestErrorNum = res.data.bsAfterTestErrorNum;
            bsAfterNormal = bsAfterTestNum - bsAfterTestErrorNum;
            // 血糖是否达标
            bsTestErrorNum = res.data.bsTestErrorNum; // 血糖未达标总数
            bsTestNum = res.data.bsTestNum; // 血糖测量总数
            bsNormal = bsTestNum - bsTestErrorNum;

            // 血压饼图
            initPie("bloodPressurePie",bpTestNum,bpTestErrorNum,bpTestNormalNum);
            // （空腹）血糖饼图
            // initPie("bloodSugarPie",bsBeforeTestNum,bsBeforeTestErrorNum,bsBeforeNormal);
            // 血糖是否达标
            initPie("bloodSugarPie",bsTestNum,bsTestErrorNum,bsNormal);
            // 餐后 2 小时血糖饼图
            initPie("AfterBloodSugarPie",bsAfterTestNum,bsAfterTestErrorNum,bsAfterNormal);

            bpError = res.data.bpError;
            if(bpError && bpError.length > 0){
                for(var i = 0;i<bpError.length;i++){

                    abnormalAgeAxis.push(bpError[i].age);

                    abnormalAgeYxis.push((bpError[i].num));

                }
            }

            initBar("abnormalAge",abnormalAgeAxis,abnormalAgeYxis,'异常人数（人）');
            bsError = res.data.bsError;
            if(bsError && bsError.length > 0){
                for(var i = 0;i<bsError.length;i++){

                    abnormalSugarAgeAxis.push(bsError[i].age);

                    abnormalSugarAgeYxis.push((bsError[i].num));

                }
            }
            initBar("abnormalSugarAge",abnormalSugarAgeAxis,abnormalSugarAgeYxis,'异常人数（人）');
        }
    });
}
$(document).ready(function(){
    getChartData();

    //多图表自适应
    window.addEventListener("resize", function () {
        $("#serviceBar").resize();
        // myEnergyChart2.resize();
    });

    // initPie("bloodPressurePie");




})